<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>双向数据绑定</title>
</head>

<body>
  <input type="text" id="input">
  <div id="app"></div>
  <script>
    const obj = {
      _name: 1
    }
    const input = document.getElementById('input')
    const app = document.getElementById('app')

    Object.defineProperty(obj, 'name', {
      get() {
        return this._name
      },
      set(val) {
        this._name = val
        app.innerHTML = this._name
      }
    })
    
    input.oninput = function () {
      obj.name = this.value
    }
  </script>
</body>

</html>